package com.ltkj.baseplatform.service.impl;

import com.ltkj.baseplatform.domain.SerSuggestion;
import com.ltkj.baseplatform.domain.vo.SerSuggestionVO;
import com.ltkj.baseplatform.mapper.SerSuggestionMapper;
import com.ltkj.baseplatform.service.ISerSuggestionService;
import com.ltkj.common.core.utils.StringUtils;
import com.ltkj.common.core.web.domain.AjaxResult;
import com.ltkj.common.security.utils.SecurityUtils;
import com.ltkj.system.api.RemoteFileService;
import com.ltkj.system.api.domain.CommAttachment;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.PlatformTransactionManager;
import org.springframework.transaction.TransactionDefinition;
import org.springframework.transaction.TransactionStatus;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.support.DefaultTransactionDefinition;

import java.util.List;

/**
 * 投诉建议Service业务层处理
 * 
 * @author chenzeen
 * @date 2023-08-29
 */
@Service
public class SerSuggestionServiceImpl implements ISerSuggestionService 
{
    @Autowired
    private SerSuggestionMapper serSuggestionMapper;

    @Autowired
    private RemoteFileService remoteFileService;

    @Autowired
    private PlatformTransactionManager transactionManager;

    private final static String SUGGESTION = "ser_suggestion";

    /**
     * 查询投诉建议
     * 
     * @param id 投诉建议主键
     * @return 投诉建议
     */
    @Override
    public SerSuggestion selectSerSuggestionById(Long id)
    {
        return serSuggestionMapper.selectSerSuggestionById(id);
    }

    @Override
    public SerSuggestionVO selectSerSuggestionVOById(Long id)
    {
        SerSuggestionVO serSuggestionVO = serSuggestionMapper.selectSerSuggestionVOById(id);
        List<CommAttachment> al = remoteFileService.selectAtta(serSuggestionVO.getId(), SUGGESTION);
        serSuggestionVO.setAttachmentList(al);
        return serSuggestionVO;
    }

    /**
     * 查询投诉建议列表
     * 
     * @param serSuggestion 投诉建议
     * @return 投诉建议
     */
    @Override
    public List<SerSuggestion> selectSerSuggestionList(SerSuggestion serSuggestion)
    {
        return serSuggestionMapper.selectSerSuggestionList(serSuggestion);
    }

    @Override
    public List<SerSuggestionVO> selectSerSuggestionListVO(SerSuggestion serSuggestion)
    {
        List<SerSuggestionVO> list = serSuggestionMapper.selectSerSuggestionListVO(serSuggestion);
        for(SerSuggestionVO vo: list) {
            List<CommAttachment> al = remoteFileService.selectAtta(vo.getId(), SUGGESTION);
            vo.setAttachmentList(al);
        }
        return list;
    }

    /**
     * 新增投诉建议
     * 
     * @param serSuggestion 投诉建议
     * @return 结果
     */
    @Override
    @Transactional
    public int insertSerSuggestion(SerSuggestion serSuggestion, String attaIds)
    {
        // 定义事务
        DefaultTransactionDefinition def = new DefaultTransactionDefinition();
        def.setPropagationBehavior(TransactionDefinition.PROPAGATION_REQUIRED);
        TransactionStatus status = transactionManager.getTransaction(def);

        serSuggestion.setUserId(""+SecurityUtils.getLoginUser().getUserid());
        serSuggestion.setCreateBy(SecurityUtils.getLoginUser().getUsername());
        int i = serSuggestionMapper.insertSerSuggestion(serSuggestion);
        if(StringUtils.isNotEmpty(attaIds)) {
            AjaxResult ar = remoteFileService.updateAttaData(serSuggestion.getId(),SUGGESTION,attaIds);
            if(ar.isError()) {
                //回滚数据
                transactionManager.rollback(status);
                return 0;
            }
        }
        // 提交事务
        transactionManager.commit(status);
        return i;
    }

    /**
     * 修改投诉建议
     * 
     * @param serSuggestion 投诉建议
     * @return 结果
     */
    @Override
    public int updateSerSuggestion(SerSuggestion serSuggestion)
    {
        serSuggestion.setUpdateBy(SecurityUtils.getLoginUser().getUsername());
        return serSuggestionMapper.updateSerSuggestion(serSuggestion);
    }

    /**
     * 批量删除投诉建议
     * 
     * @param ids 需要删除的投诉建议主键
     * @return 结果
     */
    @Override
    public int deleteSerSuggestionByIds(Long[] ids)
    {
        return serSuggestionMapper.deleteSerSuggestionByIds(ids);
    }

    /**
     * 删除投诉建议信息
     * 
     * @param id 投诉建议主键
     * @return 结果
     */
    @Override
    public int deleteSerSuggestionById(Long id)
    {
        return serSuggestionMapper.deleteSerSuggestionById(id);
    }
}
